﻿using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Runtime.InteropServices;

namespace MyApp
{
  [RunInstaller(true), ComVisible(false)]
  public partial class SsInstaller : System.Configuration.Install.Installer
  {
    public SsInstaller()
    {
      InitializeComponent();
    }
    /// <summary>
    /// This is called when installer's custom action executes and
    /// registers the toolbar which is extending a bandobject, as COM server.
    /// </summary>
    /// <param name="stateSaver"></param>     
    public override void Install(System.Collections.IDictionary stateSaver)
    {
      base.Install(stateSaver);

      try
      {
        bool isExistKey = RegistryHelper.IsRegistryKeyExist(Registry.CurrentUser, "Software\\Google\\Chrome");
        if (isExistKey)
        {
          //throw new InstallException("注册表写入错误，Chrome浏览器没有安装！");

          bool isExistNativeMsgHost = RegistryHelper.IsRegistryKeyExist(Registry.CurrentUser, "Software\\Google\\Chrome\\NativeMessagingHosts");
          if (!isExistNativeMsgHost)
          {
            RegistryHelper.CreateSubKey(Registry.CurrentUser, "Software\\Google\\Chrome\\NativeMessagingHosts");
          }

          bool isExistScreenshare = RegistryHelper.IsRegistryKeyExist(Registry.CurrentUser, "Software\\Google\\Chrome\\NativeMessagingHosts\\com.mosai.screenshare");
          if (!isExistScreenshare)
          {
            RegistryHelper.CreateSubKey(Registry.CurrentUser, "Software\\Google\\Chrome\\NativeMessagingHosts\\com.mosai.screenshare");
          }

          //定义安装目录
          string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
          //string value = Path.Combine("C:\\Screenshare", "ChromeNativeHost", "manifest.json");
          string value = Path.Combine(appDataPath, "Screenshare", "ChromeNativeHost", "manifest.json");
          RegistryHelper.SetRegistryData(Registry.CurrentUser, "Software\\Google\\Chrome\\NativeMessagingHosts\\com.mosai.screenshare", "", value);

        }
      }
      catch (Exception e)
      {
        throw new InstallException(e.Message);
      }
      
    }

    /// <summary>
    /// This is called when installer's custom action executes and
    /// registers the toolbar which is extending a bandobject, as COM server.
    /// </summary>
    /// <param name="stateSaver"></param>     
    public override void Uninstall(System.Collections.IDictionary savedState)
    {
      base.Uninstall(savedState);

      try
      {
        bool isExist = RegistryHelper.IsRegistryKeyExist(Registry.CurrentUser, "Software\\Google\\Chrome\\NativeMessagingHosts\\com.mosai.screenshare");
        if (isExist)
        {
          RegistryHelper.DeleteRegistrySubKey(Registry.CurrentUser, "Software\\Google\\Chrome\\NativeMessagingHosts\\com.mosai.screenshare");
        }        
      }
      catch (Exception e)
      {
        throw new InstallException(e.Message);
      }

    }
  }
}
